﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sasc.PV.DAL;
using System.Data.SqlClient;

namespace Sasc.PV.Web.Admin
{
    public partial class SelectJiaoSe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                var originalValue = Request[WebConstants.QueryString_LookupOrigignalValue] ?? string.Empty;
                originalValue += ",";
                BindTree(this.ListTreeView.Nodes, 0, originalValue);
            }
        }

        private void BindTree(TreeNodeCollection Nds, int IDStr, string originalValue)
        {
            var Conn = DbHelperSQL.OpenNewConnection();
            var MyCmd = new SqlCommand("select * from ERPBuMen where DirID=" + IDStr.ToString() + " order by ID asc", Conn);
            var MyReader = MyCmd.ExecuteReader();
            while (MyReader.Read())
            {
                TreeNode OrganizationNode = new TreeNode();
                OrganizationNode.Text = MyReader["BuMenName"].ToString();
                OrganizationNode.Value = MyReader["ID"].ToString();
                int strId = int.Parse(MyReader["ID"].ToString());
                OrganizationNode.ImageUrl = "~/images/user_group.gif";
                OrganizationNode.SelectAction = TreeNodeSelectAction.Expand;
                OrganizationNode.ToolTip = MyReader["BuMenName"].ToString();
                //OrganizationNode.Collapse();

                /////////////////////////////////////////////////////////////////////////////////////////////////////
                //在当前节点下加入用户    
                var allUserNodeSelected = true;

                SqlConnection Conn1 = DbHelperSQL.OpenNewConnection();
                SqlCommand MyCmd1 = new SqlCommand("select * from ERPJiaoSe  order by ID asc", Conn1);
                SqlDataReader MyReader1 = MyCmd1.ExecuteReader();
                while (MyReader1.Read())
                {
                    TreeNode UserNode = new TreeNode();
                    UserNode.Text = MyReader1["JiaoSeName"].ToString();
                    UserNode.Value = MyReader1["ID"].ToString();
                    UserNode.ToolTip = MyReader1["JiaoSeName"].ToString();
                    UserNode.SelectAction = TreeNodeSelectAction.Expand;

                    if (originalValue.IndexOf("," + MyReader1["JiaoSeName"].ToString()) >= 0)
                        UserNode.Checked = true;
                    else
                        allUserNodeSelected = false;

                    OrganizationNode.ChildNodes.Add(UserNode);
                }

                MyReader1.Close();
                Conn1.Close();

                if (allUserNodeSelected && OrganizationNode.ChildNodes.Count > 0)
                    OrganizationNode.Checked = true;

                Nds.Add(OrganizationNode);
                //递归循环
               // BindTree(Nds[Nds.Count - 1].ChildNodes, strId, originalValue);
            }
            MyReader.Close();
            Conn.Close();
        }

  
    }
}